package tv.danmaku.gtv;

//import android.support.annotation.Keep;

import android.os.Environment;

import java.io.File;
import java.io.FileWriter;

/**
 * Created by Ivy on 2018/5/21.
 */

public class GTVPlayerCacheHelper {

    private static boolean _libraryLoaded = false;

    private boolean started = false;

    static {
        if( _libraryLoaded == false ) {

            try {
                System.loadLibrary("gtvplayercache");
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }

            _libraryLoaded = true;
        }
    }

    public GTVPlayerCacheHelper() {
        started = false;
    }

    private static GTVPlayerCacheHelper _instance = null;
    public static GTVPlayerCacheHelper getInstance() {

        if( _instance == null ) {

            _instance = new GTVPlayerCacheHelper();
        }
        return _instance;
    }

    public void start(){

        synchronized (this) {

            if(started == true) {
                return;
            }

            String ijkDir = Environment.getExternalStorageDirectory() + File.separator + "gtvIjk" +File.separator;
            String cacheDir = ijkDir + "cache/";
            createDirectoryIfNeed(ijkDir);
            createDirectoryIfNeed(cacheDir);
            String configFile = ijkDir + "gtvIjk.config";
            String configSetting ="diskCacheRoot =\""+cacheDir+"\"\n";

            try {
                FileWriter fr = new FileWriter(configFile);
                fr.write(configSetting);
                fr.close();
            }catch (Exception e) {

            }


            // start
            _start(configFile);

            started = true;
        }
    }

    public void stop(){
        //stop

        _stop();
        started = false;
    }

    //static
    public void doPreload(String url, int tsindex){
        //stop

        _doPreload(url, tsindex);
    }

    public void deleteCache() {
        _deleteCache();
    }

    //static
    public long startAsyncPreload(String url, int limit){

        return _startAsyncPreload(url, limit);
    }
    public int checkAsyncPreloadFinished(long h){

        return _checkAsyncPreloadFinished(h);
    }
    public void cancelAsyncPreload(long h){

        _cancelAsyncPreload(h);
    }
    public void destroyAsyncPreload(long h){

        _destroyAsyncPreload(h);
    }

    //@Keep
    private native long _start(String config);
    //@Keep
    private native void _stop();
    //@Keep
    private native void _doPreload(String url, int tsindex);
    //@Keep
    private native void _deleteCache();

    //@Keep
    private native long _startAsyncPreload(String url, int limit);
    //@Keep
    private native int _checkAsyncPreloadFinished(long handle);
    //@Keep
    private native void _cancelAsyncPreload(long handle);
    //@Keep
    private native void _destroyAsyncPreload(long handle);


    public static boolean createDirectoryIfNeed(String dirPath) {
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return dir.exists();
    }

}
